// DlgBankUpdate.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
#include "DlgBankUpdate.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


extern CFinanceApp theApp;       


/////////////////////////////////////////////////////////////////////////////
// CDlgBankUpdate dialog


CDlgBankUpdate::CDlgBankUpdate(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgBankUpdate::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgBankUpdate)
	m_dateBegin = 0;
	m_dateEnd = 0;
	m_stcTips = _T("");
	//}}AFX_DATA_INIT

	m_iLstIdx = -1;
}


void CDlgBankUpdate::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgBankUpdate)
	DDX_DateTimeCtrl(pDX, IDC_DATE_BEG, m_dateBegin);
	DDX_DateTimeCtrl(pDX, IDC_DATE_END, m_dateEnd);
	DDX_Control(pDX, ID_BANKUPDATE, m_btnUpdate);
	DDX_Control(pDX, IDCANCEL, m_btnCancel);
	DDX_Control(pDX, IDC_CMB_OWNER, m_cmbOwner);
	DDX_Control(pDX, IDC_CMB_OPERATION, m_cmbOperation);
	DDX_Control(pDX, IDC_ED_COMMENT, m_edComment);
	DDX_Control(pDX, IDC_ED_MONEY, m_edAccount);
	DDX_Control(pDX, IDC_ED_CERIFICATE, m_edCerificate);
	DDX_Control(pDX, IDC_ED_BANKNAME, m_edBankName);
	DDX_Control(pDX, IDC_ED_BANKRATE, m_edBankRate);
	DDX_Text(pDX, IDC_TIPS, m_stcTips);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgBankUpdate, CDialog)
	//{{AFX_MSG_MAP(CDlgBankUpdate)
	ON_BN_CLICKED(ID_BANKUPDATE, OnBankupdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CDlgBankUpdate message handlers

// 
// 
//--------------------------------------------------------------
BOOL CDlgBankUpdate::OnInitDialog() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL;
	CHAR			chTmp[56];
	INT				i, iCurSel;

	
	CDialog::OnInitDialog();
	
	// <> Init UI	
	m_btnUpdate.SetIcon(IDI_EYEON, IDI_EYEOFF);
	m_btnCancel.SetIcon(IDI_CANCEL34,IDI_CANCEL32);
	
	// <> Open RecordSet
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	m_pBankset.CreateInstance(__uuidof(Recordset));

	m_edCerificate.SetLimitText( 150 );	// order by Bankbook
	m_edBankName.SetLimitText( 50 );	// 
	m_edComment.SetLimitText( 150 );	// order by Bankbook table
	
	// <> Open Bankbook by passed m_iLstIdx, waiting for update
	csSQL = "SELECT * FROM Bankbook WHERE Idx=";
	sprintf( chTmp, "%ld", m_iLstIdx );
	csSQL += chTmp;
	var = csSQL;
	try
	{
		m_pBankset->Open( var,   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	
	// <> Fill ComboBox Owner
	try
	{
		m_pRecordset->Open("SELECT * FROM Userinfo",   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbOwner.ResetContent();
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return TRUE;
		}
		
		var = m_pBankset->GetCollect("Owner");	// txt
		if(var.vt != VT_NULL)
			csSQL = (LPCSTR)_bstr_t(var);
		
		i = 0;
		iCurSel = 0;
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Name"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_cmbOwner.AddString( csTmp );
			
			if( csSQL == csTmp ) 
				iCurSel = i;	// active item
			
			m_pRecordset->MoveNext();
			i++;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	m_cmbOwner.SetCurSel( iCurSel );
	m_pRecordset->Close();


	// <> Set ComboBox Operation
	var = m_pBankset->GetCollect("Operation");	
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();

	m_cmbOperation.AddString( IDS_BANKOP_STORE );
	m_cmbOperation.AddString( IDS_BANKOP_EXTRACT );
	m_cmbOperation.SetCurSel( m_cmbOperation.FindString( -1, csTmp ) );
	

	// <> set edit account and comment
	var = m_pBankset->GetCollect("Certificate");	// nummer
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();
	m_edCerificate.SetWindowText( csTmp );
	
	var = m_pBankset->GetCollect("Bankname");	
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();
	m_edBankName.SetWindowText( csTmp );
	
	var = m_pBankset->GetCollect("Bankrate");	// nummer
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();
	m_edBankRate.SetWindowText( csTmp );
	
	var = m_pBankset->GetCollect("Account");	// nummer
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();
	m_edAccount.SetWindowText( csTmp );
	
	var = m_pBankset->GetCollect("Comment");	// txt
	if(var.vt != VT_NULL)
	{
		csTmp = (LPCSTR)_bstr_t(var);
		m_edComment.SetWindowText( csTmp );
	}
	
	// <> set start date 
	var = m_pBankset->GetCollect("StartDate");		// data/time	
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);

	// "2005/7/9" or "2005/12/31"
	m_dateBegin = DTString2CTime( csTmp ); // get date from current record

	// <> set end date 
	var = m_pBankset->GetCollect("EndDate");		// data/time	
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);

	// "2005/7/9" or "2005/12/31"
	m_dateEnd = DTString2CTime( csTmp ); // get date from current record
	
	m_stcTips = "Please modify these fields to update record";
	
	UpdateData( FALSE );

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
// OnUpdate
//--------------------------------------------------------------
void CDlgBankUpdate::OnBankupdate() 
{
	_variant_t		var;
	CTime			date;
	CString			csTmp;
	CString			csExItem[3];
	
	
	UpdateData( TRUE );

	m_edCerificate.GetWindowText( csExItem[0] );
	if( csExItem[0].IsEmpty() )
	{
		MessageBox( "Cerificate must be input!", "Remind", MB_ICONWARNING );
		return ;
	}
	m_edBankName.GetWindowText( csExItem[1] );
	if( csExItem[1].IsEmpty() )
	{
		MessageBox( "BankName must be input!", "Remind", MB_ICONWARNING );
		return ;
	}
	m_edBankRate.GetWindowText( csExItem[2] );
	if( csExItem[2].IsEmpty() )
	{
		MessageBox( "BankRate must be input!", "Remind", MB_ICONWARNING );
		return ;
	}
	m_edAccount.GetWindowText( csTmp );
	if( csTmp.IsEmpty() )
	{
		MessageBox( "Money must be input!", "Remind", MB_ICONWARNING );
		return ;
	}
	
	try
	{
		// Fill fields
		var = csTmp;
		m_pBankset->PutCollect( "Account", var );	// Account
		
		m_cmbOperation.GetLBText( m_cmbOperation.GetCurSel(), csTmp );
		var = csTmp;
		m_pBankset->PutCollect( "Operation", var );	// Operation
		
		var = csExItem[0];
		m_pBankset->PutCollect( "Certificate", var );	// Certificate

		var = csExItem[1];
		m_pBankset->PutCollect( "Bankname", var );	// Bankname
		
		var = csExItem[2];
		m_pBankset->PutCollect( "Bankrate", var );	// Bankrate
		
		date = m_dateBegin.GetTime();
		if( wtkGetSysLanguage() == WTK_BIG5 ) 
			var = date.Format( "%Y/%m/%d" );
		else
			var = date.Format( "%Y-%m-%d" );	// in GB system, is "2005-7-12"
		m_pBankset->PutCollect( "StartDate", var );	// StartDate
		
		date = m_dateEnd.GetTime();
		if( wtkGetSysLanguage() == WTK_BIG5 ) 
			var = date.Format( "%Y/%m/%d" );
		else
			var = date.Format( "%Y-%m-%d" );	// in GB system, is "2005-7-12"
		m_pBankset->PutCollect( "EndDate", var );	// EndDate
		
		m_cmbOwner.GetLBText( m_cmbOwner.GetCurSel(), csTmp );
		var = csTmp;
		m_pBankset->PutCollect( "Owner", var );	// Owner
		
		m_edComment.GetWindowText( csTmp );
		if( csTmp.IsEmpty() ) 
			var.vt = VT_NULL;
		else
			var = csTmp;	
		m_pBankset->PutCollect( "Comment", var );	// Comment
		
		m_pBankset->Update();
		
		// set OK to m_stcTips
		m_stcTips = "The current record be updated successful!";
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	// send reshow 
	CDialog::OnOK();
}

// 
//--------------------------------------------------------------
void CDlgBankUpdate::SetSelIndex(INT index)
{
	m_iLstIdx = index;
}

// 
//--------------------------------------------------------------
CDlgBankUpdate::~CDlgBankUpdate()
{
	if( m_pRecordset != NULL )
	{
		if( m_pRecordset->State )
			m_pRecordset->Close();
		m_pRecordset = NULL;
	}
	
	if( m_pBankset != NULL )
	{
		if( m_pBankset->State )
			m_pBankset->Close();
		m_pBankset = NULL;
	}
}
